// pages/my/comment.js
import {
    hotel,
    store,
    gourmet
} from '../../api/apiData'
var pageNum1 = 1; //页码

Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        value: 5,
        loadMoreText: '加载中...', //底部文字
        list: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.active){
            this.setData({
                active:parseInt(options.active)
             })
        }
       
        this.getList(1)

    },
    toPre(e){
        console.log(e)
        let img = e.currentTarget.dataset.images
        let item = e.currentTarget.dataset.item
        wx.previewImage({
          urls: img,
          current:item
        })
    },
    toUrl(e){
        let url = e.currentTarget.dataset.url
        wx.navigateTo({
          url: url,
        })
    },
    onChange(e){
         this.setData({
            active:e.detail.index
         })
         this.onPullDownRefresh()
    },
    // 获取列表
    getList(page) {
        let that = this
        if(that.data.active==0){
            hotel.comment.myList({
                page: page,
                size: 10,
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    that.setData({
                        list: that.data.list.concat(res.data.list) //合并数据
                    })
                    if (11 > res.data.list.length > 0) {
                        that.setData({
                            loadMoreText: '' //没有更多了
                        })
                    }
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        }
        if(that.data.active==1){
            store.comment.myList({
                page: page,
                size: 10,
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    that.setData({
                        list: that.data.list.concat(res.data.list) //合并数据
                    })
                    if (11 > res.data.list.length > 0) {
                        that.setData({
                            loadMoreText: '' //没有更多了
                        })
                    }
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        }
        if(that.data.active==2){
            gourmet.comment.myList({
                page: page,
                size: 10,
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    that.setData({
                        list: that.data.list.concat(res.data.list) //合并数据
                    })
                    if (11 > res.data.list.length > 0) {
                        that.setData({
                            loadMoreText: '' //没有更多了
                        })
                    }
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        pageNum1 += 1;
        this.getList(pageNum1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})